<?php
declare (strict_types=1);
/**
 * Created by PhpStorm.
 * User: Mmx
 * Date: 2021\06\08
 * Time: 09:30
 */

namespace app\api\controller\v5;

use app\common\models\Course;
use app\common\models\Datum;
use app\common\models\DatumCate;
use app\common\models\Grade;
use app\common\models\Record;
use app\common\models\Error;
use think\db\Query;

class Index extends BaseController
{
    /**
     * 学习中心
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'grade' => $this->grade(),   //获取年级信息
            'course' => $this->course(),   //获取课程分类信息
            'book' => $this->book(),   //获取课本名称信息
        ]);
    }

    /**
     *点击标记
     *
     * @return \think\Response
     */
    public function sign()
    {
        //条件
        $id = input('id/d');
        $datum_cate_id = input('datum_cate_id/d');
        if (empty($id)) {
            return errMsg(400, '请选择标记id', []);
        }
        $model = new Error();

        if (!empty($model->where(['datum_id' => $id])->where('api_member_id', $this->user_id)->where('datum_cate_id', $datum_cate_id)->find())) {
            return errMsg(500, '请勿重复添加', []);
        }
        //查询
        $data1 = Datum::where(['id' => $id])
            ->field('id,title,English,American,video,desc,example,translate,else')
            ->find()->toArray();
        $data2 = DatumCate::where(['id' =>  $datum_cate_id])
            ->field('grade_id,course_id')
            ->find()->toArray();
        //重命名
//        $data2['name'] = $data2['title'];
//        unset($data2['title']);
        //赋值
        $data1['api_member_id'] = $this->user_id;
        $data1['datum_id'] = $id;
        $data1['datum_cate_id'] = $datum_cate_id;
        $data1['type'] = 1;
        unset($data1['id']);
        //合并
        $data = array_merge($data1, $data2);
        //dd($data);
        //存库
        $arr = $model->save($data);
        //返回
        return sucessMsg(200, '加入成功', ['data' => $arr]);
    }

    /**
     * 年级
     *
     * @return \think\Response
     */
    public function grade()
    {
        $data = Grade::where(['status' => 1])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return $data;
    }

    /**
     * 课程
     *
     * @return \think\Response
     */
    public function course()
    {
        $id = input('id/s', 1);
        $data = Course::where(['status' => 1, 'grade_id' => $id])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return $data;
    }

    /**
     * 学习中心列表
     */
    public function book()
    {
        $page = input('page/d', 1); // 页数
        $pageSize = input('pageSize/d', 12);    //  每页显示条数
        $grade_id = input('grade_id/d', 1);     // 年级id
        $course_id = input('course_id/d');     // 年级id
        //学习中心列表
        $list = DatumCate::where(['status' => 1, 'grade_id' => $grade_id])
            ->where(function (Query $query) use ($course_id) {
                if ($course_id)
                    $query->where('course_id', $course_id);
            })
            ->withAttr('image', function ($val, $data) {
                return strtolower(config("app.domain_url") . str_replace('\\', '/', $val));
            })
            ->order(['grade_id' => 'asc'])
            ->page($page, $pageSize)
            ->select()->toArray();
        foreach ($list as $k => $v) {
            //$a['datum_cate_id'] = $v['id'];
            $grade_total = Datum::where('datum_cate_id', $v['id'])->count();
            $grade_work_total = Record::where('datum_cate_id', $v['id'])->where(['api_member_id' => $this->user_id])->count();
            if ($grade_total == 0 || $grade_work_total == 0) {
                $list[$k]['grade_work_total'] = 0;
            } else {
                $list[$k]['grade_work_total'] = round($grade_work_total / $grade_total * 100);
            }
        }
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => ['list' => $list, 'total' => count($list)]];
    }
}

